/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package accountmanager;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author jonesbr
 */
public class MoneyTransfer implements Runnable {

    private Account fromAccount;
    private Account toAccount;
    private double transferAmount;

    /**
     * Transfer money. wait when there is no money to transfer.
     */
    @Override
    public void run() {
        // inifinite loop
        while (true) {
            synchronized (this) {
                try {
                    // wait for a notification.
                    wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(MoneyTransfer.class.getName()).log(Level.SEVERE, null, ex);
                }
                // transfer money.
                fromAccount.setBalance(fromAccount.getBalance() - transferAmount);
                toAccount.setBalance(toAccount.getBalance() + transferAmount);
            }
        }


    }

    /**
     * @return the fromAccount
     */
    public Account getFromAccount() {
        return fromAccount;
    }

    /**
     * @param fromAccount the fromAccount to set
     */
    public void setFromAccount(Account fromAccount) {
        this.fromAccount = fromAccount;
    }

    /**
     * @return the toAccount
     */
    public Account getToAccount() {
        return toAccount;
    }

    /**
     * @param toAccount the toAccount to set
     */
    public void setToAccount(Account toAccount) {
        this.toAccount = toAccount;
    }

    /**
     * @return the transferAmount
     */
    public double getTransferAmount() {
        return transferAmount;
    }

    /**
     * @param transferAmount the transferAmount to set
     */
    public void setTransferAmount(double transferAmount) {
        this.transferAmount = transferAmount;
    }
}
